﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _23_1_16SQL_TEST
{
    
    public partial class Form1 : Form
    {
        public class ComboBoxItem
        {
            public ComboBoxItem(int id, string text)
            {
                ID = id;
                Text = text;
            }
            public int ID { get; private set; }
            public string Text { get; private set; }
            public override string ToString()
            {
                return Text;
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = textBox1.Text;
                    con.Open();
                    InitializeCategories(con);
                }
            }
            
            catch (SqlException) {

                MessageBox.Show("Fail");
            }
            
        }
        private void InitializeCategories(SqlConnection connection)
        {
            using (SqlCommand cmd = connection.CreateCommand()) {

                cmd.CommandText = "SELECT [Category ID], [Category Name] FROM Category";
                cmd.CommandType = CommandType.Text;
                using(SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        int categoryId = reader.GetInt32(0);
                        string categoryName = reader.GetString(1);
                        cbCategory.Items.Add(new ComboBoxItem(categoryId, categoryName));
                    }
                }
            }

        }
    }
}
